'''
Created on Oct 16, 2012

@author: gyucheol
'''

import sys
from github import Github
from github import Issue
from github import Repository
from datetime import datetime

# Github User information
GITHUB_USER_ID = "jangho87"
GITHUB_USER_PW = "qwer1234"

# Github Bootstrap Repository information
GITHUB_BOOTSTRAP_ORG_NAME = "twitter"
GITHUB_BOOTSTRAP_REPOSITORY_NAME = "bootstrap"

# Github Bootstrap Issue filtering in the repository
ISSUE_BOOTSTRAP_URL = "https://github.com/twitter/bootstrap/issues/"
ISSUE_BOOTSTRAP_START = 1
ISSUE_BOOTSTRAP_END = 5478

# Github Homebrew Repository information
GITHUB_HOMEBREW_ORG_NAME = "mxcl"
GITHUB_HOMEBREW_REPOSITORY_NAME = "homebrew"

# Github Homebrew Issue filtering in the repository
ISSUE_HOMEBREW_URL = "https://github.com/mxcl/homebrew/issues/"
ISSUE_HOMEBREW_START = 1
ISSUE_HOMEBREW_END = 15389 

# Github Rails Repository information
GITHUB_RAILS_ORG_NAME = "rails"
GITHUB_RAILS_REPOSITORY_NAME = "rails"

# Github Rails Issue filtering in the repository
ISSUE_RAILS_URL = "https://github.com/rails/rails/issues/"
ISSUE_RAILS_START = 1
ISSUE_RAILS_END = 7898

# Github Html5 Repository information
GITHUB_HTML5_ORG_NAME = "h5bp"
GITHUB_HTML5_REPOSITORY_NAME = "html5-boilerplate"

# Github HTML5 Issue filtering in the repository
ISSUE_HTML5_URL = "https://github.com/h5bp/html5-boilerplate/issues/"
ISSUE_HTML5_START = 1
ISSUE_HTML5_END = 1235

# Github OH_MY_ZSH Repository information
GITHUB_OMZ_ORG_NAME = "robbyrussell"
GITHUB_OMZ_REPOSITORY_NAME = "oh-my-zsh"

# Github OMZ Issue filtering in the repository
ISSUE_OMZ_URL = "https://github.com/robbyrussell/oh-my-zsh/issues/"
ISSUE_OMZ_START = 1
ISSUE_OMZ_END = 1361

# Github NODE Repository information
GITHUB_NODE_ORG_NAME = "joyent"
GITHUB_NODE_REPOSITORY_NAME = "node"

# Github NODE Issue filtering in the repository
ISSUE_NODE_URL = "https://github.com/joyent/node/issues/"
ISSUE_NODE_START = 1
ISSUE_NODE_END = 4151

# Flags for export file name
BOOTSTRAP_FILE_NAME = 'bootstrap_num_of_complete_issues_per_month.txt'
HOMEBREW_FILE_NAME = 'homebrew_num_of_complete_issues_per_month.txt'
RAILS_FILE_NAME = 'rails_num_of_complete_issues_per_month.txt'
HTML5_FILE_NAME = 'html5_num_of_complete_issues_per_month.txt'
OMZ_FILE_NAME = 'oh_my_zsh_num_of_complete_issues_per_month.txt'
NODE_FILE_NAME = 'node_num_of_complete_issues_per_month.txt'

if __name__ == '__main__':
    pass

#g = Github(GITHUB_USER_ID, GITHUB_USER_PW)
g = Github(GITHUB_USER_ID, GITHUB_USER_PW)
#print g.rate_limiting


# This part is for getting number of project tasks from bootstrap
try:
    bootstrap_file = open(BOOTSTRAP_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_organization(GITHUB_BOOTSTRAP_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_BOOTSTRAP_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_BOOTSTRAP_START, ISSUE_BOOTSTRAP_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url
            
            if issue.closed_at == None:
                continue
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        bootstrap_file.write(key + ", " + str(value) + '\n')
    
    bootstrap_file.close()
    
except IOError:
    print('File Error') 
# End of bootstrap


# This part is for getting number of project tasks from homebrew
try:
    homebrew_file = open(HOMEBREW_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_user(GITHUB_HOMEBREW_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_HOMEBREW_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_HOMEBREW_START, ISSUE_HOMEBREW_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url

            if issue.closed_at == None:
                continue            
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        homebrew_file.write(key + ", " + str(value) + '\n')
   
    homebrew_file.close()
except IOError:
    print('File Error') 
# End of rails

# This part is for getting number of project tasks from rails
try:
    rails_file = open(RAILS_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_organization(GITHUB_RAILS_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_RAILS_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_RAILS_START, ISSUE_RAILS_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url

            if issue.closed_at == None:
                continue
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        rails_file.write(key + ", " + str(value) + '\n')
   
    rails_file.close()
except IOError:
    print('File Error') 
# End of rails

    
# This part is for getting number of project tasks from html5
try:
    start_remain = str(g.rate_limiting)
    html5_file = open(HTML5_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_organization(GITHUB_HTML5_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_HTML5_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_HTML5_START, ISSUE_HTML5_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url
            
            if issue.closed_at == None:
                continue            
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        html5_file.write(key + ", " + str(value) + '\n')
   
    end_remain = str(g.rate_limiting)
    print end_remain + "/" + start_remain + ' request remaining'
    html5_file.close()
except IOError:
    print('File Error') 
# End of HTML5

# This part is for getting number of project tasks from oh_my_zsh
try:
    omz_file = open(OMZ_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_user(GITHUB_OMZ_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_OMZ_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_OMZ_START, ISSUE_OMZ_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url

            if issue.closed_at == None:
                continue            
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        omz_file.write(key + ", " + str(value) + '\n')
   
    omz_file.close()
except IOError:
    print('File Error') 
# End of oh_my_zsh

# This part is for getting number of project tasks from node
try:
    node_file = open(NODE_FILE_NAME, 'w')

    # This part is for collecting rails reopened issue
    org = g.get_organization(GITHUB_NODE_ORG_NAME)
    print org.url
    
    repo = org.get_repo(GITHUB_NODE_REPOSITORY_NAME)    
    num_of_issues = dict()
    
    for i in range(ISSUE_NODE_START, ISSUE_NODE_END+1):
        try: 
            issue = repo.get_issue(i)
            print issue.url

            if issue.closed_at == None:
                continue
            
            closed_time = issue.closed_at
            time = str(closed_time.year) + "/" + str(closed_time.month)
            
            if time not in num_of_issues.keys():
                num_of_issues[time] = 1
            else:
                num_of_issues[time] = num_of_issues.get(time,1) + 1

            print num_of_issues.items()
        except Exception as inst:
            print inst
            
    print num_of_issues.items()
    
    for key, value in num_of_issues.items():
        node_file.write(key + ", " + str(value) + '\n')
   
    node_file.close()
except IOError:
    print('File Error') 
# End of node